/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proffline.sqlite;

import com.proffline.conexion.ConexionJDBC;
import com.proffline.gui.VentanaPrincipal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

/**
 *
 * @author Rolando
 */
public class ResultExecute {

    ConexionJDBC conn = new ConexionJDBC();
    Connection connection = null;
    ResultSet rs = null;
    Statement stmt = null;

    public void ejecutarConsulta(List<String> queries) throws Exception {
        connection = conn.obtenerConexionSQL();
        final boolean oldAutoCommit = connection.getAutoCommit();
        stmt = connection.createStatement();
        connection.setAutoCommit(false);
        rs = null;
        try {
            VentanaPrincipal.obtenerInstancia().setMaximoProgresoParcial(queries.size());
            int contador = 0;
            for (String query : queries) {
                stmt.execute(query);
                VentanaPrincipal.obtenerInstancia().setProgresoParcial(contador++);
            }
            VentanaPrincipal.obtenerInstancia().setProgresoParcial(queries.size());
        } catch (Exception e) {
            connection.rollback();
            throw e;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
            }
            if (conn != null) {
                try {
                    connection.commit();
                    connection.setAutoCommit(oldAutoCommit);
                    conn.close();
                } catch (Exception e) {
                }
            }
        }

    }
}
